'use client'
import PetActions from './pet/PetActions'
import PetStatus from './pet/PetStatus'
import usePetState from '@/hooks/usePetState'

export default function VirtualPet() {
  const {
    happiness,
    exp,
    level,
    energy,
    action,
    message,
    setMessage,
    setAction,
    setHappiness,
    setEnergy,
    saveData,
    addExp
  } = usePetState()

  // useEffect(() => {
  //   const savedData = localStorage.getItem('virtualPet')
  //   if (savedData) {
  //     const { happiness, exp, level, energy } = JSON.parse(savedData)
  //     setHappiness(happiness)
  //     setExp(exp)
  //     setLevel(level)
  //     setEnergy(energy || 100)
  //   }

  //   // 每30分钟降低快乐度和能量
  //   const timer = setInterval(() => {
  //     setHappiness(prev => {
  //       const newHappiness = Math.max(0, prev - 1)
  //       setEnergy(prevEnergy => {
  //         const newEnergy = Math.max(0, prevEnergy - 2)
  //         saveData(newHappiness, exp, level, newEnergy)
  //         return newEnergy
  //       })
  //       return newHappiness
  //     })
  //   }, 30 * 60 * 1000)

  //   return () => clearInterval(timer)
  // }, [])

  
  const handlePet = () => {
    if (energy < 5) {
      setMessage('太累了，需要休息...')
      return
    }
    setAction('happy')
    setHappiness(prev => {
      const newHappiness = Math.min(100, prev + 10)
      setEnergy(prevEnergy => {
        const newEnergy = Math.max(0, prevEnergy - 5)
        saveData(newHappiness, exp, level, newEnergy)
        return newEnergy
      })
      return newHappiness
    })
    setMessage('好开心！')
    addExp(5)
    setTimeout(() => {
      setAction('idle')
      setMessage('')
    }, 1000)
  }
  const increaseHappiness = () => {
    if (energy < 5) {
      setMessage('太累了，需要休息...')
      return
    }
    setAction('happy')
    setHappiness(prev => {
      const newHappiness = Math.min(100, prev + 10)
      setEnergy(prevEnergy => {
        const newEnergy = Math.max(0, prevEnergy - 5)
        saveData(newHappiness, exp, level, newEnergy)
        return newEnergy
      })
      return newHappiness
    })
    setMessage('好开心！')
    addExp(5)
    setTimeout(() => {
      setAction('idle')
      setMessage('')
    }, 1000)
  }


  // 添加新的动作函数
  const playGame = () => {
    if (energy < 20) {
      setMessage('太累了，需要休息...')
      return
    }
    setAction('playing')
    setHappiness(prev => {
      const newHappiness = Math.min(100, prev + 15)
      setEnergy(prevEnergy => {
        const newEnergy = Math.max(0, prevEnergy - 20)
        saveData(newHappiness, exp, level, newEnergy)
        return newEnergy
      })
      return newHappiness
    })
    setMessage('玩游戏真开心！')
    addExp(10)
    setTimeout(() => {
      setAction('idle')
      setMessage('')
    }, 2000)
  }

  const sleep = () => {
    setAction('sleeping')
    setMessage('睡个好觉~')
    const recovery = setInterval(() => {
      setEnergy(prev => {
        const newEnergy = Math.min(100, prev + 10)
        saveData(happiness, exp, level, newEnergy)
        return newEnergy
      })
    }, 1000)
    
    setTimeout(() => {
      clearInterval(recovery)
      setAction('idle')
      setMessage('精力充沛！')
      setTimeout(() => setMessage(''), 1000)
    }, 5000)
  }

  const drink = () => {
    if (energy < 10) {
      setMessage('太累了，需要休息...')
      return
    }
    setAction('drinking')
    setHappiness(prev => {
      const newHappiness = Math.min(100, prev + 5)
      setEnergy(prevEnergy => {
        const newEnergy = Math.max(0, prevEnergy - 5)
        saveData(newHappiness, exp, level, newEnergy)
        return newEnergy
      })
      return newHappiness
    })
    setMessage('喝杯咖啡真舒服~')
    setTimeout(() => {
      setAction('idle')
      setMessage('')
    }, 1500)
  }

 
  return (
    <div className="fixed top-1/2 right-4 transform -translate-y-1/2 bg-white p-4 rounded-lg shadow-lg">
      <div className="relative">
        <PetActions 
          action={action}
          happiness={happiness}
          onPet={handlePet}
          onPlay={playGame}
          onSleep={sleep}
          onDrink={drink}
        />
        {message && (
          <div className="absolute -top-8 right-0 bg-gray-800 text-white px-2 py-1 rounded text-sm">
            {message}
          </div>
        )}
      </div>
      <PetStatus 
        happiness={happiness}
        energy={energy}
        level={level}
        exp={exp}
      />
    </div>
  )
}